<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>DOM | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/frontend_js基础_DOM.md.C7jmMA7R.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>js基础知识</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js基础/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>js基础知识</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/DOM.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>DOM</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Map.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Map</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Promise.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Promise</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Set.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Set</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Symbol.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Symbol</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E4%BA%8B%E4%BB%B6.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>事件</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>任务管理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>函数进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E7%BB%A7%E6%89%BF.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>原型与继承</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>基本类型</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%AF%B9%E8%B1%A1.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>对象</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>常用技巧</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>数组类型</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%A8%A1%E5%9D%97%E8%AE%BE%E8%AE%A1.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>模块设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>正则表达式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E7%A9%BA%E9%97%B4%E5%9D%90%E6%A0%87.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>空间坐标</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E7%B1%BB.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>类</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E8%BF%AD%E4%BB%A3%E5%99%A8.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>迭代器</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _frontend_js%E5%9F%BA%E7%A1%80_DOM" data-v-39a288b8><div><h1 id="dom" tabindex="-1">DOM <a class="header-anchor" href="#dom" aria-label="Permalink to &quot;DOM&quot;">​</a></h1><p>DOM 即 Document Object Model 文档对象模型，浏览器在加载页面是会生成 DOM 对象，以供我们使用 JS 控制页面元素。</p><h3 id="文档渲染" tabindex="-1">文档渲染 <a class="header-anchor" href="#文档渲染" aria-label="Permalink to &quot;文档渲染&quot;">​</a></h3><p>浏览器会将 HTML 文本内容进行渲染，并生成相应的 JS 对象，同时会对不符规则的标签进行处理。</p><ul><li>浏览器会将标签规范后渲染页面</li><li>目的一让页面可以正确呈现</li><li>目的二可以生成统一的 JS 可操作对象</li></ul><h3 id="操作时机" tabindex="-1">操作时机 <a class="header-anchor" href="#操作时机" aria-label="Permalink to &quot;操作时机&quot;">​</a></h3><p>需要保证浏览器已经渲染了内容才可以读取的节点对象，下例将无法读取到节点对象</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  const node = document.getElementById(&#39;love&#39;)</span></span>
<span class="line"><span style="color:#ABB2BF;">  console.log(node) //null</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#D19A66;font-style:italic;"> id</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>不过我们可以将脚本通过事件放在页面渲染完执行</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  window.onload = () =&gt; </span><span style="color:#C678DD;">{</span></span>
<span class="line"><span style="color:#E06C75;">    const</span><span style="color:#E06C75;"> node</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">node</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#D19A66;font-style:italic;"> id</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>或使用定时器将脚本设置为异步执行</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  setTimeout(() =&gt; </span><span style="color:#C678DD;">{</span></span>
<span class="line"><span style="color:#E06C75;">    const</span><span style="color:#E06C75;"> node</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">node</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  }</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#D19A66;font-style:italic;"> id</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>或将脚本设置在外部文件并使用 defer 属性加载，defer 即会等到 DOM 解析后迟延执行</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#D19A66;font-style:italic;"> defer</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;defer&quot;</span><span style="color:#D19A66;font-style:italic;"> src</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;love.js&quot;</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;font-style:italic;"> id</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">&gt;&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="节点对象" tabindex="-1">节点对象 <a class="header-anchor" href="#节点对象" aria-label="Permalink to &quot;节点对象&quot;">​</a></h3><p>JS 中操作 DOM 的内容称为节点对象（node)，即然是对象就包括操作 NODE 的属性和方法</p><ul><li>包括 12 种类型的节点对象</li><li>常用了节点为 document、标签元素节点、文本节点、注释节点</li><li>节点均继承自 Node 类型，所以拥有相同的属性或方法</li><li>document 是 DOM 操作的起始节点</li></ul><table tabindex="0"><thead><tr><th>节点</th><th>nodeType</th><th>nodeName</th><th>nodeValue</th><th>示例</th></tr></thead><tbody><tr><td><strong>元素节点（Element Nodes）</strong></td><td>1</td><td>大写标签名</td><td>不包含值或返回null‌</td><td><code> &lt;div&gt;、&lt;p&gt;、&lt;a&gt;</code></td></tr><tr><td><strong>属性节点（Attribute Nodes）</strong></td><td>2</td><td>属性的名称</td><td>属性的值‌</td><td><code>id href class</code></td></tr><tr><td><strong>文本节点（Text Nodes）</strong></td><td>3</td><td>#text</td><td>内容</td><td></td></tr><tr><td><strong>CDATA区段节点（CDATASection Nodes）</strong></td><td>4</td><td></td><td></td><td>仅存在XML文档中</td></tr><tr><td><strong>注释节点（Comment Nodes）</strong></td><td>8</td><td>#comment</td><td>注释的内容‌</td><td></td></tr><tr><td><strong>文档节点（Document Nodes）</strong></td><td>9</td><td>#document</td><td>null</td><td></td></tr><tr><td><strong>文档类型节点</strong></td><td>10</td><td>doctype的名称</td><td>null</td><td><code>&lt;!DOCTYPE html&gt;</code></td></tr></tbody></table><h3 id="原型链" tabindex="-1">原型链 <a class="header-anchor" href="#原型链" aria-label="Permalink to &quot;原型链&quot;">​</a></h3><table tabindex="0"><thead><tr><th>原型</th><th>说明</th></tr></thead><tbody><tr><td>Object</td><td>根对象，提供 hasOwnProperty 等基本对象操作支持</td></tr><tr><td>EventTarget</td><td>提供 addEventListener、removeEventListener 等事件支持方法</td></tr><tr><td>Node</td><td>提供 firstChild、parentNode 等节点操作方法</td></tr><tr><td>Element</td><td>提供 getElementsByTagName、querySelector 等方法</td></tr><tr><td>HTMLElement</td><td>所有元素的基础类，提供 childNodes、nodeType、nodeName、className、nodeName 等方法</td></tr><tr><td>HTMLHeadingElement</td><td>Head 标题元素类</td></tr></tbody></table><p>打印原型链</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> prototype</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">el</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#C678DD;">    const</span><span style="color:#E5C07B;"> prototypes</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> []</span></span>
<span class="line"><span style="color:#E5C07B;">    prototypes</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">push</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">el</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    prototypes</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">push</span><span style="color:#ABB2BF;">(...(</span><span style="color:#E5C07B;">el</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#C678DD;"> ?</span><span style="color:#61AFEF;"> prototype</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">el</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">:</span><span style="color:#ABB2BF;"> []))</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#E06C75;"> prototypes</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="对象特征" tabindex="-1">对象特征 <a class="header-anchor" href="#对象特征" aria-label="Permalink to &quot;对象特征&quot;">​</a></h3><p>利用<code>Object.assign</code>添加或者修改属性。</p><h2 id="常用节点" tabindex="-1">常用节点 <a class="header-anchor" href="#常用节点" aria-label="Permalink to &quot;常用节点&quot;">​</a></h2><p>JS 提供了访问常用节点的 api</p><table tabindex="0"><thead><tr><th>方法</th><th>说明</th></tr></thead><tbody><tr><td>document</td><td>document 是 DOM 操作的起始节点</td></tr><tr><td>document.documentElement</td><td>文档节点即 html 标签节点</td></tr><tr><td>document.body</td><td>body 标签节点</td></tr><tr><td>document.head</td><td>head 标签节点</td></tr><tr><td>document.links</td><td>超链接集合</td></tr><tr><td>document.anchors</td><td>所有锚点集合</td></tr><tr><td>document.forms</td><td>form 表单集合</td></tr><tr><td>document.images</td><td>图片集合</td></tr></tbody></table><h3 id="document" tabindex="-1">DOCUMENT <a class="header-anchor" href="#document" aria-label="Permalink to &quot;DOCUMENT&quot;">​</a></h3><p>document 是 window 对象的属性，是由 HTMLDocument 类实现的实例。</p><h2 id="节点属性" tabindex="-1">节点属性 <a class="header-anchor" href="#节点属性" aria-label="Permalink to &quot;节点属性&quot;">​</a></h2><p>使用 data 属性可以获取文本与注释内容</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;font-style:italic;"> id</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  love.com</span></span>
<span class="line"><span style="color:#ABB2BF;">  &lt;!-- love 注释内容--&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  const app = document.querySelector(&#39;#app&#39;)</span></span>
<span class="line"><span style="color:#ABB2BF;">  console.log(app.childNodes[0].data)</span></span>
<span class="line"><span style="color:#ABB2BF;">  console.log(app.childNodes[1].data)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="节点集合" tabindex="-1">节点集合 <a class="header-anchor" href="#节点集合" aria-label="Permalink to &quot;节点集合&quot;">​</a></h2><p>Nodelist 与 HTMLCollection 都是包含多个节点标签的集合，大部分功能也是相同的。</p><ul><li>getElementsByClassName 等方法返回的是 NodeList</li><li>querySelectorAll 返回的是 HTMLCollection</li><li>NodeList 节点列表是动态的，即内容添加后会动态更新</li></ul><h3 id="item" tabindex="-1">item <a class="header-anchor" href="#item" aria-label="Permalink to &quot;item&quot;">​</a></h3><p>Nodelist 与 HTMLCollection 提供了 item()方法来根据索引获取元素，也可以使用数组索引。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  const nodes = document.getElementsByTagName(&#39;div&#39;)</span></span>
<span class="line"><span style="color:#ABB2BF;">  console.dir(nodes.item(0))</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  const nodes = document.getElementsByTagName(&#39;div&#39;)</span></span>
<span class="line"><span style="color:#ABB2BF;">  console.dir(nodes[0])</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="nameditem" tabindex="-1">namedItem <a class="header-anchor" href="#nameditem" aria-label="Permalink to &quot;namedItem&quot;">​</a></h3><p>HTMLCollection 具有 namedItem 方法可以按 name 或 id 属性来获取元素</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> name</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  &lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">&gt;houdunren.com&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  &lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> name</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;you&quot;</span><span style="color:#ABB2BF;">&gt;houdunwang.com&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  const</span><span style="color:#E5C07B;"> nodes</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementsByTagName</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;div&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">dir</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">nodes</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">namedItem</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#E5C07B;">   console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">dir</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">nodes</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">namedItem</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;you&#39;</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>数字索引时使用 item 方法，字符串索引时使用 namedItem 或 items 方法</p><h2 id="动态与静态" tabindex="-1">动态与静态 <a class="header-anchor" href="#动态与静态" aria-label="Permalink to &quot;动态与静态&quot;">​</a></h2><p>通过 getElementsByTagname 等 getElementsBy... 函数获取的 Nodelist 与 HTMLCollection 集合是动态的，即有元素添加或移动操作将实时反映最新状态。</p><ul><li>使用 getElement...返回的都是动态的集合</li><li>使用 querySelectorAll 返回的是静态集合</li></ul><h3 id="动态特性" tabindex="-1">动态特性 <a class="header-anchor" href="#动态特性" aria-label="Permalink to &quot;动态特性&quot;">​</a></h3><p>下例中通过按钮动态添加元素后，获取的元素集合是动态的，而不是上次获取的固定快照。</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;baidu.com&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;jd.com&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">button</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;add&quot;</span><span style="color:#ABB2BF;">&gt;添加元素&lt;/</span><span style="color:#E06C75;">button</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> elements</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementsByTagName</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;h1&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">elements</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> button</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;#add&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  button</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">addEventListener</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;click&#39;</span><span style="color:#ABB2BF;">, () </span><span style="color:#C678DD;">=&gt;</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#E5C07B;">    document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;body&#39;</span><span style="color:#ABB2BF;">).</span><span style="color:#61AFEF;">insertAdjacentHTML</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;beforeend&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;&lt;h1&gt;love you&lt;/h1&gt;&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">elements</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">  })</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>document.querySelectorAll 获取的集合是静态的</p><h2 id="遍历节点" tabindex="-1">遍历节点 <a class="header-anchor" href="#遍历节点" aria-label="Permalink to &quot;遍历节点&quot;">​</a></h2><h3 id="forof" tabindex="-1">forOf <a class="header-anchor" href="#forof" aria-label="Permalink to &quot;forOf&quot;">​</a></h3><p>Nodelist 与 HTMLCollection 是类数组的可迭代对象所以可以使用 for...of 进行遍历</p><h3 id="foreach" tabindex="-1">forEach <a class="header-anchor" href="#foreach" aria-label="Permalink to &quot;forEach&quot;">​</a></h3><p>Nodelist 节点列表也可以使用 forEach 来进行遍历，但 HTMLCollection 则不可以</p><h3 id="call-apply" tabindex="-1">call/apply <a class="header-anchor" href="#call-apply" aria-label="Permalink to &quot;call/apply&quot;">​</a></h3><p>节点集合对象原型中不存在 map 方法，但可以借用 Array 的原型 map 方法实现遍历</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E5C07B;">Array</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">map</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">call</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">nodes</span><span style="color:#ABB2BF;">, (</span><span style="color:#E06C75;font-style:italic;">node</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">index</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">=&gt;</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">node</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">index</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">})</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="array-from" tabindex="-1">Array.from <a class="header-anchor" href="#array-from" aria-label="Permalink to &quot;Array.from&quot;">​</a></h3><p>Array.from 用于将类数组转为组件，并提供第二个迭代函数。所以可以借用 Array.from 实现遍历</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E5C07B;"> Array</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">from</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">nodes</span><span style="color:#ABB2BF;">, (</span><span style="color:#E06C75;font-style:italic;">node</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">index</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">=&gt;</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">node</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">index</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">})</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="节点关系" tabindex="-1">节点关系 <a class="header-anchor" href="#节点关系" aria-label="Permalink to &quot;节点关系&quot;">​</a></h2><p>下面是通过节点关系获取相应元素的方法</p><table tabindex="0"><thead><tr><th>节点属性</th><th>说明</th></tr></thead><tbody><tr><td>childNodes</td><td>获取所有子节点</td></tr><tr><td>parentNode</td><td>获取父节点</td></tr><tr><td>firstChild</td><td>第一个子节点</td></tr><tr><td>lastChild</td><td>最后一个子节点</td></tr><tr><td>nextSibling</td><td>下一个兄弟节点</td></tr><tr><td>previousSibling</td><td>上一个兄弟节点</td></tr></tbody></table><h2 id="标签关系" tabindex="-1">标签关系 <a class="header-anchor" href="#标签关系" aria-label="Permalink to &quot;标签关系&quot;">​</a></h2><p>使用 childNodes 等获取的节点包括文本与注释，但这不是我们常用的，为此系统也提供了只操作元素的关系方法。</p><h3 id="基础知识" tabindex="-1">基础知识 <a class="header-anchor" href="#基础知识" aria-label="Permalink to &quot;基础知识&quot;">​</a></h3><p>下面是处理标签关系的常用 API</p><table tabindex="0"><thead><tr><th>节点属性</th><th>说明</th></tr></thead><tbody><tr><td>parentElement</td><td>获取父元素</td></tr><tr><td>children</td><td>获取所有子元素</td></tr><tr><td>childElementCount</td><td>子标签元素的数量</td></tr><tr><td>firstElementChild</td><td>第一个子标签</td></tr><tr><td>lastElementChild</td><td>最后一个子标签</td></tr><tr><td>previousElementSibling</td><td>上一个兄弟标签</td></tr><tr><td>nextElementSibling</td><td>下一个兄弟标签</td></tr><tr><td>contains</td><td>返回布尔值，判断传入的节点是否为该节点的后代节点</td></tr></tbody></table><h2 id="标签获取" tabindex="-1">标签获取 <a class="header-anchor" href="#标签获取" aria-label="Permalink to &quot;标签获取&quot;">​</a></h2><h3 id="getelementbyid" tabindex="-1">getElementById <a class="header-anchor" href="#getelementbyid" aria-label="Permalink to &quot;getElementById&quot;">​</a></h3><p>使用 ID 选择是非常方便的选择具有 ID 值的节点元素，但注意 ID 应该是唯一的，getElementById 只能通过 document 访问。</p><h3 id="getelementsbyname" tabindex="-1">getElementsByName <a class="header-anchor" href="#getelementsbyname" aria-label="Permalink to &quot;getElementsByName&quot;">​</a></h3><p>使用 getElementByName 获取设置了 name 属性的元素。</p><ul><li>返回 NodeList 节点列表对象</li><li>NodeList 顺序为元素在文档中的顺序</li><li>需要在 document 对象上使用</li></ul><h3 id="getelementsbytagname" tabindex="-1">getElementsByTagName <a class="header-anchor" href="#getelementsbytagname" aria-label="Permalink to &quot;getElementsByTagName&quot;">​</a></h3><p>使用 getElementsByTagName 用于按标签名获取元素</p><ul><li>返回 HTMLCollection 节点列表对象</li><li>是不区分大小的获取</li></ul><h3 id="getelementsbyclassname" tabindex="-1">getElementsByClassName <a class="header-anchor" href="#getelementsbyclassname" aria-label="Permalink to &quot;getElementsByClassName&quot;">​</a></h3><p>getElementsByClassName 用于按 class 样式属性值获取元素集合</p><ul><li>设置多个值时顺序无关，指包含这些 class 属性的元素</li></ul><h2 id="样式选择器" tabindex="-1">样式选择器 <a class="header-anchor" href="#样式选择器" aria-label="Permalink to &quot;样式选择器&quot;">​</a></h2><h3 id="queryselectorall" tabindex="-1">querySelectorAll <a class="header-anchor" href="#queryselectorall" aria-label="Permalink to &quot;querySelectorAll&quot;">​</a></h3><p>使用 querySelectorAll 根据 CSS 选择器获取 Nodelist 节点列表</p><ul><li>获取的 NodeList 节点列表是静态的，添加或删除元素后不变</li></ul><h3 id="queryselector" tabindex="-1">querySelector <a class="header-anchor" href="#queryselector" aria-label="Permalink to &quot;querySelector&quot;">​</a></h3><p>querySelector 使用 CSS 选择器获取一个元素。</p><h3 id="matches" tabindex="-1">matches <a class="header-anchor" href="#matches" aria-label="Permalink to &quot;matches&quot;">​</a></h3><p>用于检测元素是否是指定的样式选择器匹配</p><h3 id="closest" tabindex="-1">closest <a class="header-anchor" href="#closest" aria-label="Permalink to &quot;closest&quot;">​</a></h3><p>查找最近的符合选择器的祖先元素（包括自身）</p><h2 id="标准属性" tabindex="-1">标准属性 <a class="header-anchor" href="#标准属性" aria-label="Permalink to &quot;标准属性&quot;">​</a></h2><h3 id="属性别名" tabindex="-1">属性别名 <a class="header-anchor" href="#属性别名" aria-label="Permalink to &quot;属性别名&quot;">​</a></h3><p>有些属性名与 JS 关键词冲突，系统已经起了别名</p><table tabindex="0"><thead><tr><th>属性</th><th>别名</th></tr></thead><tbody><tr><td>class</td><td>className</td></tr><tr><td>for</td><td>htmlFor</td></tr></tbody></table><h2 id="元素特征" tabindex="-1">元素特征 <a class="header-anchor" href="#元素特征" aria-label="Permalink to &quot;元素特征&quot;">​</a></h2><p>对于标准的属性可以使用 DOM 属性的方式进行操作，但对于标签的非标准的定制属性则不可以。但 JS 提供了方法来控制标准或非标准的属性</p><p>可以理解为元素的属性分两个地方保存，DOM 属性中记录标准属性，特征中记录标准和定制属性</p><ul><li>使用特征操作时属性名称不区分大小写</li><li>特征值都为字符串类型</li></ul><table tabindex="0"><thead><tr><th>方法</th><th>说明</th></tr></thead><tbody><tr><td>getAttribute</td><td>获取属性</td></tr><tr><td>setAttribute</td><td>设置属性</td></tr><tr><td>removeAttribute</td><td>删除属性</td></tr><tr><td>hasAttribute</td><td>属性检测</td></tr></tbody></table><p>特征是可迭代对象，下面使用 for...of 来进行遍历操作</p><h3 id="attributes" tabindex="-1">attributes <a class="header-anchor" href="#attributes" aria-label="Permalink to &quot;attributes&quot;">​</a></h3><p>元素提供了 attributes 属性可以只读的获取元素的属性</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#D19A66;"> data-content</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;静思&quot;</span><span style="color:#ABB2BF;">&gt;love.com&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> houdunwang</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;.love&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">dir</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">houdunwang</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">attributes</span><span style="color:#ABB2BF;">[</span><span style="color:#98C379;">&#39;data-content&#39;</span><span style="color:#ABB2BF;">].</span><span style="color:#E06C75;">nodeValue</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//静思</span></span>
<span class="line"><span style="color:#E5C07B;">	console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">dir</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">houdunwang</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">attributes</span><span style="color:#ABB2BF;">[</span><span style="color:#98C379;">&#39;data-content&#39;</span><span style="color:#ABB2BF;">].</span><span style="color:#E06C75;">value</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//静思</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="自定义特征" tabindex="-1">自定义特征 <a class="header-anchor" href="#自定义特征" aria-label="Permalink to &quot;自定义特征&quot;">​</a></h3><p>虽然可以随意定义特征并使用 getAttribute 等方法管理，但很容易造成与标签的现在或未来属性重名。建议使用以 data-为前缀的自定义特征处理，针对这种定义方式 JS 也提供了接口方便操作。</p><ul><li>元素中以 data-为前缀的属性会添加到属性集中</li><li>使用元素的 dataset 可获取属性集中的属性</li></ul><h3 id="属性同步" tabindex="-1">属性同步 <a class="header-anchor" href="#属性同步" aria-label="Permalink to &quot;属性同步&quot;">​</a></h3><p>下面对 input 值使用属性设置，但并没有同步到特征</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">input</span><span style="color:#D19A66;"> type</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;text&quot;</span><span style="color:#D19A66;"> name</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;package&quot;</span><span style="color:#D19A66;"> value</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;love.com&quot;</span><span style="color:#ABB2BF;"> /&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  const</span><span style="color:#E5C07B;"> package</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">`[name=&#39;package&#39;]`</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  package</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">value</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &#39;love.com&#39;</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">package</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getAttribute</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;value&#39;</span><span style="color:#ABB2BF;">))</span><span style="color:#7F848E;font-style:italic;">//love.com</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>但改变 input 的特征 value 会同步到 DOM 对象属性</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">input</span><span style="color:#D19A66;"> type</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;text&quot;</span><span style="color:#D19A66;"> name</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;package&quot;</span><span style="color:#D19A66;"> value</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;love.com&quot;</span><span style="color:#ABB2BF;"> /&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  const</span><span style="color:#E5C07B;"> package</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">`[name=&#39;package&#39;]`</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  package</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">setAttribute</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;value&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;love.com&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">package</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">value</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//love.com</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="创建节点" tabindex="-1">创建节点 <a class="header-anchor" href="#创建节点" aria-label="Permalink to &quot;创建节点&quot;">​</a></h2><p>创建节点的就是构建出 DOM 对象，然后根据需要添加到其他节点中</p><h3 id="createelement" tabindex="-1">createElement <a class="header-anchor" href="#createelement" aria-label="Permalink to &quot;createElement&quot;">​</a></h3><p>使用 createElement 方法可以标签节点对象&gt;</p><p>使用 PROMISE 结合节点操作来加载外部 JAVASCRIPT 文件</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> js</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">file</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#C678DD;">  return</span><span style="color:#C678DD;"> new</span><span style="color:#E5C07B;"> Promise</span><span style="color:#ABB2BF;">((</span><span style="color:#E06C75;font-style:italic;">resolve</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">reject</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">=&gt;</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#C678DD;">    let</span><span style="color:#E06C75;"> js</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">createElement</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;script&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    js</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">type</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &#39;text/javascript&#39;</span></span>
<span class="line"><span style="color:#E5C07B;">    js</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">src</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> file</span></span>
<span class="line"><span style="color:#E5C07B;">    js</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">onload</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> resolve</span></span>
<span class="line"><span style="color:#E5C07B;">    js</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">onerror</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> reject</span></span>
<span class="line"><span style="color:#E5C07B;">    document</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">head</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">appendChild</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">js</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">  })</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">js</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;outside.js&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">  .</span><span style="color:#61AFEF;">then</span><span style="color:#ABB2BF;">(() </span><span style="color:#C678DD;">=&gt;</span><span style="color:#E5C07B;"> console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;加载成功&#39;</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#ABB2BF;">  .</span><span style="color:#61AFEF;">catch</span><span style="color:#ABB2BF;">((</span><span style="color:#E06C75;font-style:italic;">error</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">=&gt;</span><span style="color:#E5C07B;"> console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">`</span><span style="color:#C678DD;">${</span><span style="color:#E5C07B;">error</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">target</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">src</span><span style="color:#C678DD;">}</span><span style="color:#98C379;"> 加载失败`</span><span style="color:#ABB2BF;">))</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="clonenode-importnode" tabindex="-1">cloneNode&amp;importNode <a class="header-anchor" href="#clonenode-importnode" aria-label="Permalink to &quot;cloneNode&amp;importNode&quot;">​</a></h3><p>使用 cloneNode 和 document.importNode 用于复制节点对象操作</p><ul><li>cloneNode 是节点方法</li><li>cloneNode 参数为 true 时递归复制子节点即深拷贝</li><li>importNode 是 documet 对象方法</li></ul><p>复制 div#app 节点并添加到 body 元素中</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;#app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> newApp</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">cloneNode</span><span style="color:#ABB2BF;">(</span><span style="color:#D19A66;">true</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  document</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">body</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">appendChild</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">newApp</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>document.importNode 方法是部分 IE 浏览器不支持的，也是复制节点对象的方法</p><ul><li>第一个参数为节点对象</li><li>第二个参数为 true 时递归复制</li></ul><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;#app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> newApp</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">importNode</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">app</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">true</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  document</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">body</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">appendChild</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">newApp</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="节点内容" tabindex="-1">节点内容 <a class="header-anchor" href="#节点内容" aria-label="Permalink to &quot;节点内容&quot;">​</a></h2><h3 id="innerhtml" tabindex="-1">innerHTML <a class="header-anchor" href="#innerhtml" aria-label="Permalink to &quot;innerHTML&quot;">​</a></h3><p>inneHTML 用于向标签中添加 html 内容，同时触发浏览器的解析器重绘 DOM。</p><ul><li>innerHTML 中只解析 HTML 标签语法，所以其中的 script 不会做为 JS 处理</li></ul><h3 id="outerhtml" tabindex="-1">outerHTML <a class="header-anchor" href="#outerhtml" aria-label="Permalink to &quot;outerHTML&quot;">​</a></h3><p>outerHTML 与 innerHTML 的区别是包含父标签</p><h3 id="textcontent-与-innertext" tabindex="-1">textContent 与 innerText <a class="header-anchor" href="#textcontent-与-innertext" aria-label="Permalink to &quot;textContent 与 innerText&quot;">​</a></h3><p>textContent 与 innerText 是访问或添加文本内容到元素中</p><ul><li>textContentb 部分 IE 浏览器版本不支持</li><li>innerText 部分 FireFox 浏览器版本不支持</li><li>获取时忽略所有标签,只获取文本内容</li><li>设置时将内容中的标签当文本对待不进行标签解析</li></ul><p>设置时将标签当文本对待，即转为 HTML 实体内容</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  &lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#D19A66;"> data</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;hd&quot;</span><span style="color:#ABB2BF;">&gt;love.com&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;#app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  app</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">textContent</span><span style="color:#56B6C2;">=</span><span style="color:#98C379;">&quot;&lt;h1&gt;love&lt;/h1&gt;&quot;</span><span style="color:#7F848E;font-style:italic;"> //&lt;h1&gt;love&lt;/h1&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="outertext" tabindex="-1">outerText <a class="header-anchor" href="#outertext" aria-label="Permalink to &quot;outerText&quot;">​</a></h3><p>与 innerText 差别是会影响所操作的标签</p><h3 id="insertadjacenttext" tabindex="-1">insertAdjacentText <a class="header-anchor" href="#insertadjacenttext" aria-label="Permalink to &quot;insertAdjacentText&quot;">​</a></h3><p>将文本插入到元素指定位置，不会对文本中的标签进行解析，包括以下位置</p><table tabindex="0"><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>beforebegin</td><td>元素本身前面</td></tr><tr><td>afterend</td><td>元素本身后面</td></tr><tr><td>afterbegin</td><td>元素内部前面</td></tr><tr><td>beforeend</td><td>元素内部后面</td></tr></tbody></table><h2 id="节点管理" tabindex="-1">节点管理 <a class="header-anchor" href="#节点管理" aria-label="Permalink to &quot;节点管理&quot;">​</a></h2><h3 id="推荐方法" tabindex="-1">推荐方法 <a class="header-anchor" href="#推荐方法" aria-label="Permalink to &quot;推荐方法&quot;">​</a></h3><table tabindex="0"><thead><tr><th>方法</th><th>说明</th></tr></thead><tbody><tr><td>append</td><td>节点尾部添加新节点或字符串</td></tr><tr><td>prepend</td><td>节点开始添加新节点或字符串</td></tr><tr><td>before</td><td>节点前面添加新节点或字符串</td></tr><tr><td>after</td><td>节点后面添加新节点或字符串</td></tr><tr><td>replaceWith</td><td>将节点替换为新节点或字符串</td></tr></tbody></table><p>将 h2 移动到 h1 之前</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;first.com@h1&lt;/</span><span style="color:#E06C75;">h1</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;second@h2&lt;/</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> h1</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;h1&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> h2</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">querySelector</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;h2&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  h1</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">before</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">h2</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="insertadjacenthtml" tabindex="-1">insertAdjacentHTML <a class="header-anchor" href="#insertadjacenthtml" aria-label="Permalink to &quot;insertAdjacentHTML&quot;">​</a></h3><p>将 html 文本插入到元素指定位置，浏览器会对文本进行标签解析，包括以下位置</p><table tabindex="0"><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>beforebegin</td><td>元素本身前面</td></tr><tr><td>afterend</td><td>元素本身后面</td></tr><tr><td>afterbegin</td><td>元素内部前面</td></tr><tr><td>beforeend</td><td>元素内部后面</td></tr></tbody></table><h3 id="insertadjacentelement" tabindex="-1">insertAdjacentElement <a class="header-anchor" href="#insertadjacentelement" aria-label="Permalink to &quot;insertAdjacentElement&quot;">​</a></h3><p>insertAdjacentElement() 方法将指定元素插入到元素的指定位置，包括以下位置</p><ul><li>第一个参数是位置</li><li>第二个参数为新元素节点</li></ul><table tabindex="0"><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>beforebegin</td><td>元素本身前面</td></tr><tr><td>afterend</td><td>元素本身后面</td></tr><tr><td>afterbegin</td><td>元素内部前面</td></tr><tr><td>beforeend</td><td>元素内部后面</td></tr></tbody></table><p>两者区别就是插入类型不同，<code>insertAdjacentElement</code>插入的元素需要继承<code>type &#39;Element&#39;</code>.</p><h2 id="样式管理" tabindex="-1">样式管理 <a class="header-anchor" href="#样式管理" aria-label="Permalink to &quot;样式管理&quot;">​</a></h2><p>通过 DOM 修改样式可以通过更改元素的 class 属性或通过 style 对象设置行样式来完成。</p><ul><li>建议使用 class 控制样式，将任务交给 CSS 处理，更简单高效</li></ul><h3 id="classlist" tabindex="-1">classList <a class="header-anchor" href="#classlist" aria-label="Permalink to &quot;classList&quot;">​</a></h3><p>如果对类单独进行控制使用 classList 属性操作</p><table tabindex="0"><thead><tr><th>方法</th><th>说明</th></tr></thead><tbody><tr><td>node.classList.add</td><td>添加类名</td></tr><tr><td>node.classList.remove</td><td>删除类名</td></tr><tr><td>node.classList.toggle</td><td>切换类名</td></tr><tr><td>node.classList.contains</td><td>类名检测</td></tr></tbody></table><p>在元素的原有 class 上添加新 class</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;d-flex container&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">classList</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">add</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//love为类名作为参数</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>使用 classList 也可以移除 class 列表中的部分 class</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;d-flex container&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">classList</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">remove</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;container&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>使用 toggle 切换类，即类已经存在时删除，不存在时添加</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;d-flex container&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">addEventListener</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;click&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#C678DD;">function</span><span style="color:#ABB2BF;"> () {</span></span>
<span class="line"><span style="color:#E5C07B;">    this</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">classList</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">toggle</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">  })</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>使用 contains 检查 class 是否存在</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#D19A66;"> class</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;d-flex container&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">  let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">classList</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">contains</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;container&#39;</span><span style="color:#ABB2BF;">)) </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">classList</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">contains</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;love&#39;</span><span style="color:#ABB2BF;">)) </span><span style="color:#7F848E;font-style:italic;">//false</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="设置行样式" tabindex="-1">设置行样式 <a class="header-anchor" href="#设置行样式" aria-label="Permalink to &quot;设置行样式&quot;">​</a></h3><p>使用 style 对象可以对样式属性单独设置，使用 cssText 可以批量设置行样式</p><p><strong>样式属性设置</strong></p><p>使用节点的 style 对象来设置行样式</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">backgroundColor</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &#39;red&#39;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><strong>批量设置行样式</strong></p><p>使用 cssText 属性可以批量设置行样式，属性名和写 CSS 一样不需要考虑驼峰命名</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">cssText</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> `background-color:red;color:yellow`</span></span>
<span class="line"><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">setAttribute</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;style&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">`background-color:red;color:yellow;`</span><span style="color:#ABB2BF;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="获取样式" tabindex="-1">获取样式 <a class="header-anchor" href="#获取样式" aria-label="Permalink to &quot;获取样式&quot;">​</a></h3><p>可以通过 style 对象，<code>window.getComputedStyle</code> 对象获取样式属性，下面进行说明</p><p><strong>style</strong></p><p>可以使用 DOM 对象的 style 属性读取行样式</p><ul><li>style 对象不能获取行样式外定义的样式</li></ul><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">backgroundColor</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">margin</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">marginTop</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">color</span><span style="color:#ABB2BF;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>getComputedStyle</strong></p><p>使用 window.getComputedStyle 可获取所有应用在元素上的样式属性</p><ul><li>函数第一个参数为元素</li><li>第二个参数为伪类</li><li>这是计算后的样式属性，所以取得的单位和定义时的可能会有不同</li></ul><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E06C75;">    div</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#ABB2BF;">        font-size: </span><span style="color:#D19A66;">35</span><span style="color:#E06C75;">px</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">        color: </span><span style="color:#D19A66;">yellow</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">style</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">div</span><span style="color:#D19A66;"> id</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;app&quot;</span><span style="color:#D19A66;"> style</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;background-color: red; margin: 20px;&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">div</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#C678DD;">    let</span><span style="color:#E06C75;"> app</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> document</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getElementById</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;app&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">    let</span><span style="color:#E06C75;"> style</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> window</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getComputedStyle</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">app</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">fontSize</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">slice</span><span style="color:#ABB2BF;">(</span><span style="color:#D19A66;">0</span><span style="color:#ABB2BF;">, </span><span style="color:#56B6C2;">-</span><span style="color:#D19A66;">2</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#61AFEF;">parseInt</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">fontSize</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#E5C07B;">    app</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">fontSize</span><span style="color:#56B6C2;"> =</span><span style="color:#61AFEF;"> parseInt</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">fontSize</span><span style="color:#ABB2BF;">) </span><span style="color:#56B6C2;">+</span><span style="color:#D19A66;"> 21</span><span style="color:#56B6C2;"> +</span><span style="color:#98C379;"> &#39;px&#39;</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#61AFEF;">parseInt</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">fontSize</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/frontend/js基础/" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>js基础知识</span><!--]--></a></div><div class="pager" data-v-e257564d><a class="VPLink link pager-link next" href="/frontend/js%E5%9F%BA%E7%A1%80/Map.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>下一篇</span><span class="title" data-v-e257564d>Map</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>